using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Musicman.Entities;
using Musicman.Business;

namespace MusicmanWeb.Custom
{
    public partial class MyLoginControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] != null && HttpContext.Current.User.Identity.IsAuthenticated)
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;
                Musicman.Entities.User user = DataManager.Instance.GetUserInfo((long)Session["UserID"]);
                int userLogins = DataManager.Instance.GetLoginCount((long)user.Personid);
                int totalLogins = DataManager.Instance.GetTotalLoginCount();
                this.Label1.Text = String.Format("Welcome {0} {1}", user.Firstname, user.Lastname);
                this.Label2.Text = String.Format("Logging stats {0} | {1}", userLogins, totalLogins);
            }
            else
            {
                this.Panel1.Visible = true;
                this.Panel2.Visible = false;
            }
        }


        protected void Login_Click(object sender, EventArgs e)
        {
            
            // Session["userID"] = user;
            // FormsAuthentication.RedirectFromLoginPage(user.Emailaddress,false);

            User user = DataManager.Instance.LoggInUser(this.UserName.Text, this.Password.Text, true);

            if (user != null)
            {
                // Session["LOGGED"] = String.Format("{0} {1}", user.Firstname, user.Lastname);
                Session["UserID"] = user.Personid;
                FormsAuthentication.RedirectFromLoginPage(user.Emailaddress, false);
                
            }
            
            

            
            // Session["muser"] = muser;
            // HttpContext.Current.User = muser;
            
            Response.Redirect("Default.aspx");
        }
    }
}